package com.example.temp.controller;

import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.example.temp.ai.AI;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/ai")
public class AIcontroller {
    
    /**
     * 获取AI助手的默认回复
     */
    @GetMapping("/greeting")
    public ResponseEntity<Map<String, Object>> getAIGreeting() {
        Map<String, Object> response = new HashMap<>();
        try {
            GenerationResult result = AI.callWithMessage("你是谁");
            if (result != null && result.getOutput() != null 
                    && result.getOutput().getChoices() != null 
                    && !result.getOutput().getChoices().isEmpty()) {
                String reply = result.getOutput().getChoices().get(0).getMessage().getContent();
                response.put("success", true);
                response.put("data", reply);
                return ResponseEntity.ok(response);
            }
            response.put("success", false);
            response.put("message", "未获取到AI回复");
            return ResponseEntity.badRequest().body(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "调用AI服务失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    /**
     * 向AI提问
     * @param requestData 用户问题
     */
    @PostMapping("/ask")
    public ResponseEntity<Map<String, Object>> askAI(@RequestBody Map<String, Object> requestData) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 将问题数据转换为 JSON 字符串
            ObjectMapper objectMapper = new ObjectMapper();
            String questionJson = objectMapper.writeValueAsString(requestData);
            
            // 构造更详细的提示词
            String prompt = "你是一个教授数据库课程的教师助手，在打分时严格按给定答案判分，不要参考答案以外信息。请根据以下题目信息，为每个题目提供评分和评语。\n\n" + questionJson + "\n\n" +
                        //  "评分标准：\n" +
                        //  "1. 完全正确：满分\n" +
                        //  "2. 部分正确：满分的60%-90%\n" +
                        //  "3. 答案不完整：满分的20%-50%\n" +
                        //  "4. 完全错误：0分\n\n" +
                         "请严格按照以下格式返回结果（不要添加其他内容）：\n" +
                         "为每个题目按序号返回，格式如下：\n" +
                         "(1) score：[分数]分 comment：[具体评语]\n" +
                         "(2) score：[分数]分 comment：[具体评语]\n" +
                         "(3) score：[分数]分 comment：[具体评语]\n" +
                         "...\n" +
                         "(N) score：[分数]分 comment：[具体评语]\n\n" +
                         "评语要求：\n" +
                         "1. 明确指出答案是否正确\n" +
                         "2. 说明给分理由\n" +
                         "3. 语言简洁明了\n" +
                         "4. 必须为每个题目提供建议，题目序号从1开始连续编号";
            
            GenerationResult result = AI.callWithMessage(prompt);
            if (result != null && result.getOutput() != null 
                    && result.getOutput().getChoices() != null 
                    && !result.getOutput().getChoices().isEmpty()) {
                response.put("success", true);
                response.put("data", result.getOutput().getChoices().get(0).getMessage().getContent());
            } else {
                response.put("success", false);
                response.put("message", "No response from AI");
            }
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "Error calling AI service: " + e.getMessage());
            e.printStackTrace();
        }
        return ResponseEntity.ok(response);
    }
}
